var pos = [];
var atSameTimeN;
var isFailed;
window.highestScore = 0;
window.isPause = false;
module.exports = pos;

cc.Class({
    extends: cc.Component,

    properties: {
        GameUI: cc.Node,

        numPrefab:
        {
            default: null,
            type: cc.Prefab
        },

        scoreDisplay: {
            default: null,
            type: cc.Label
        },

        Anime321: {
            default: null,
            type: cc.Animation
        },


        timeDisplay: cc.Label,
        timeIcon: cc.Animation,
        AddMinusSeconds: cc.Animation,
        failScoreDisplay: cc.Label,
        failedUI: cc.Animation,
        pauseScoreDisplay: cc.Label,
        pauseUI: cc.Animation,
        pauseUINode: cc.Node,
        Resume321: cc.Animation,

        atSameN: 0,
        initTime: 0,

        
        RightAudio: {
            default: null,
            type: cc.AudioClip,
        },

        WrongAudio: {
            default: null,
            type: cc.AudioClip,
        },
        FailAudio: {
            default: null,
            type: cc.AudioClip,
        },
        

    },
    playRightAudio() {
        if (window.isMute != true) {
            cc.audioEngine.play(this.RightAudio, false, 1);    
        }
        
    },
    playWrongAudio() {
        if (window.isMute != true) {
            cc.audioEngine.play(this.WrongAudio, false, 1);
        }
        
    },
    openAudio() {
        cc.find('UI_pause/audio_off_button').active = false;
        cc.find('UI_pause/audio_on_button').active = true;
        window.isMute = false;
        console.log("NOT MUTE");
    },
    muteAudio() {
        cc.find('UI_pause/audio_off_button').active = true;
        cc.find('UI_pause/audio_on_button').active = false;
        window.isMute = true;
        console.log("MUTE");
    },

    // LIFE-CYCLE CALLBACKS:


    onLoad() {
        atSameTimeN = this.atSameN;
        cc.log('at game on scene! ');
        cc.log(cc.props)
        window.timerValue = cc.props.initTimer;
        window.isPause = false;

        if (window.isMute == true) {
            this.muteAudio();
        }
        else {
            this.openAudio();
        }

        pos.splice(0, pos.length);//清空pos数组的值
        isFailed = 0;//重置状态

        this.score = 0;
        this.nowNumber = 1;

        this.timeDisplay.string = window.timerValue;           //  场景文本框为 显示5

        var animGameUI = this.GameUI.getComponent(cc.Animation);
        var anim321 = this.Anime321.getComponent(cc.Animation);
        var anim321State = anim321.getAnimationState('321');

        animGameUI.play('gameUI_load');
        anim321.play('321');
        if (anim321State) {
            anim321State.on('stop', (event) => {
                // 处理停止播放时的逻辑
                for (let i = 0; i < atSameTimeN; i++) {
                    this.createNewNumber();
                }
                if (window.timerValue >= 0) {
                    this.schedule(function () {
                        this.jishi();
                    }, 1);
                }
                anim321State.off('stop', (event), this);
            }, this);
        }
    },

    jishi() {                // 倒计时算法
        if (window.timerValue >= 1 && window.isPause == false) {
            window.timerValue = window.timerValue - 1;
            this.timeDisplay.string = window.timerValue;    //场景中文本框显示 
            // cc.log("window.timerValue=" + window.timerValue);
        }
    },

    changetimer(newtime, addorminus) {
        this.timeDisplay.string = newtime;
        var animUpdateTime = this.timeIcon.getComponent(cc.Animation);
        var animAddMinusSeconds = this.AddMinusSeconds.getComponent(cc.Animation);
        if (addorminus == 1) {
            animUpdateTime.play('update_time');
            this.AddMinusSeconds.getComponent(cc.Label).string = ('+' + 8 + 's');
            animAddMinusSeconds.play('+-s');
        }
        else {
            animUpdateTime.play('update_time');
            this.AddMinusSeconds.getComponent(cc.Label).string = ('-' + 5 + 's');
            animAddMinusSeconds.play('+-s');
        }
    },
    update(dt) {
        if (window.timerValue <= 0 && isFailed == 0) {
            isFailed = 1;
            this.failed();
        }
        // cc.log(this.nowNumber-atSameTimeN);
    },

    onPause: function () {
        if (window.isPause == false) {
            window.isPause = true;
            cc.log("pause");

            var anim321 = this.Anime321.getComponent(cc.Animation);
            var anim321State = anim321.getAnimationState('321');
            if (anim321State.isPlaying) {
                anim321.pause('321');
                // cc.log("pause 321 successfully");
            }
            this.pauseScoreDisplay.string = this.score;
            if (window.highestScore < this.score) {
                window.highestScore = this.score;
            }
            var animPauseUI = this.pauseUI.getComponent(cc.Animation);
            animPauseUI.play('pauseUI_appear');



        }
    },

    onResume() {
        var anim321 = this.Anime321.getComponent(cc.Animation);
        var anim321State = anim321.getAnimationState('321');

        var animPauseUI = this.pauseUI.getComponent(cc.Animation);
        animPauseUI.play('pauseUI_disappear');

        if (anim321State.isPlaying) {

            anim321.resume('321');

            window.isPause = false;

        }
        else if (isFailed == 0) {
            var animResume321 = this.Resume321.getComponent(cc.Animation);
            animResume321.play('r321');
            // cc.log("play r321 successfully");

        }
        cc.log('resume');
    },


    failed() {
        window.isPause = true;
        if (window.isMute != true)
            cc.audioEngine.play(this.FailAudio, false, 1);
        this.failScoreDisplay.string = this.score;
        console.log("Fail");
        var animFailedUI = this.failedUI.getComponent(cc.Animation);
        animFailedUI.play('failedUI_appear');
        if (window.highestScore < this.score) {
            window.highestScore = this.score;
        }
    },


    createNewNumber: function () {
        var newNum = cc.instantiate(this.numPrefab);
        newNum.getComponent('Number').game = this;
        newNum.parent = this.node;
        newNum.getComponent("Number").init();
        var label = newNum.getChildByName('label');
        label.getComponent(cc.Label).string = this.nowNumber;
        this.nowNumber++;
    },

    addScore: function () {
        this.score += 1;
        this.scoreDisplay.string = this.score;
    },
});


